package com.simplemvc.core.util;

import com.simplemvc.core.vo.ActionItem;
import com.simplemvc.core.vo.ActionResult;
import com.simplemvc.core.vo.Actions;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 *
 * @author 王海
 */
public class ActionMapping {
   
    private static final Map<String,ActionItem> actionMapping = new ConcurrentHashMap<String, ActionItem>();
    
    private static final Map<String,String> resultMap = new ConcurrentHashMap<String, String>();
    
    static {
        try {
            Actions actions =  (Actions) XmlBeanConverter.xml2Bean("simplemvc.xml", Actions.class);
            for (ActionItem item : actions.items) {
                actionMapping.put(item.getRequestMapping(),item);
                List<ActionResult> results = item.getResults();
                for (ActionResult result : results) {
                    resultMap.put(item.getRequestMapping()+":"+result.getName(), result.getUrl());
                }
            }
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
           ex.printStackTrace();;
        }
    }
    
    public static final ActionItem getActionItem(String path){
        return actionMapping.get(path);
    }
    
    public static final String getResultUrl(String path,String result){
        return resultMap.get(path+":"+result);
    }
}
